package io.renren.modules.seller.controller;

import io.renren.modules.seller.entity.GoodsEntity;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.group.Goods;
import io.renren.modules.seller.service.GoodsService;
import io.renren.modules.sys.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商家
 * @author Administrator
 */
@Slf4j
@RestController
@RequestMapping("seller/goods")
public class GoodsSellerController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 返回全部列表
     *
     * @return GoodsEntities
     */
    @RequestMapping("/findAll")
    public List<GoodsEntity> findAll() {
        return goodsService.list();
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(String page, String rows) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", rows);
        return goodsService.findPage(params);
    }


    /**
     * 获取实体
     *
     * @param id id
     * @return Goods
     */
    @RequestMapping("/findOne")
    public Goods findOne(Long id) {
        return goodsService.findOne(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            goodsService.deleteByIds(ids);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody GoodsEntity goods, String page, String rows) {
        // 获取商家ID
        Long userId = ShiroUtils.getUserId();
        goods.setSellerId(userId.toString());
        return goodsService.findPage(goods, page, rows);
    }

    /**
     * 修改
     *
     * @param goods goods
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Goods goods) {
        //当前商家ID
        String sellerId = ShiroUtils.getUserId().toString();
        //首先判断商品是否是该商家的商品
        Goods goods2 = goodsService.findOne(goods.getGoods().getId());
        if (!goods2.getGoods().getSellerId().equals(sellerId) || !goods.getGoods().getSellerId().equals(sellerId)) {
            return new Result(false, "非法操作");
        }
        try {
            goodsService.update(goods);
            return Result.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("修改失败");
        }
    }

    /**
     * 增加
     *
     * @param goods goods
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Goods goods) {
        //获取商家ID
        String sellerId = ShiroUtils.getUserId().toString();
        goods.getGoods().setSellerId(sellerId);
        try {
            goodsService.add(goods);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "增加失败");
        }
    }

}
